<?php

namespace ShowStop\TelevisionBundle\Controller;

use Symfony\Component\Validator\Constraints\Date;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;


class SearchController extends Controller
{
	public function formAction() {
		//create the search form
		//TODO: create search form service
		$form = $this->createFormBuilder( array( 'searchBox' => 'Find a Show' ) )
		->add('searchBox', 'text' )
		->getForm();
		
		return $this->render('ShowStopTelevisionBundle:Search:form.html.twig', array( 'form' => $form->createView() ) );
	}
	
	public function searchAction(Request $request) {
		$query = "No Search Query Entered";
		 
		if( $request->getMethod() == 'GET' ) {
			$showName = $_GET["q"];
	
			$em = $this->getDoctrine()->getEntityManager();
			 
			$query = $em->createQuery( "SELECT s FROM ShowStopTelevisionBundle:Series s WHERE s.name LIKE '%" . $showName ."%'");
			 
			$results = $query->getResult();
	
		}
		 
		return $this->render('ShowStopTelevisionBundle:Search:search.html.twig', array('name' => $showName, 'results' => $results ) );
	}
	
	public function searchAllAction( $index ) {
		 
		$query = "No Search Query Entered";
		 
		$em = $this->getDoctrine()->getEntityManager();
	
		if($index == '0' ){
			$query = $em->createQuery( "SELECT s FROM ShowStopTelevisionBundle:Series s ");
			$results = $query->getResult();
		}
		else if ( $index == '#'){
	
			$conn = $this->get('database_connection');
			$results = $conn->fetchAll( "SELECT * FROM series WHERE name REGEXP '^[?!@#$%^&*0-9]'");
	
		}
		else {
			$query = $em->createQuery( "SELECT s FROM ShowStopTelevisionBundle:Series s WHERE s.name like '". $index ."%'");
			$results = $query->getResult();
		}
		 
		$headerLinks = array("#","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
		 
		return $this->render('ShowStopTelevisionBundle:Search:searchAll.html.twig', array( 'headerLinks'=>$headerLinks, 'results' => $results, 'index' => $index ) );
	}
	

}

?>